<template>
	<view class="center-page">
		<u-navbar title="创作中心" :autoBack="true" :placeholder="true">
			<view class="u-nav-slot" slot="right">
				<u-icon name="camera" color="#999" size="30" @click="gonavto"></u-icon>
			</view>
		</u-navbar>
		<view class="product">
			<view class="product-title">
				产品图
			</view>
			<view class="tips">
				请上传清晰的白底图片
			</view>
		</view>
		<view class="upload">
			<u-upload width="100%" height="229px" :fileList="fileList" @afterRead="afterRead" name="1" multiple
				:maxCount="1"></u-upload>
		</view>
		<view class="illustrate">
			为了您有更好的产品体验，请上传清晰的产品白底图片，部分照片模版除外
		</view>
		<view class="example">
			<u--image :showLoading="true" src="/static/home/shafas.webp" width="100%" height="450rpx"></u--image>
		</view>
		<view class="example-text">
			示例图
		</view>
		<view class="btn">
			<u-button shape="circle" size="large" color="#000" type="primary" text="下一步 1/3"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList: [],
			}
		},
		methods: {
			gonavto() {
				uni.navigateTo({
					url: '/pages/index/video/details'
				})
			},
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.center-page {
		padding: 30rpx;

		.product {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 30rpx;

			.product-title {
				font-size: 32rpx;
				color: #333;
				font-weight: 700;
			}

			.tips {
				color: #999;
				font-size: 30rpx;
			}
		}

		.upload {
			padding: 30rpx 0;
		}

		.illustrate {
			font-size: 28rpx;
			color: #999;
		}

		.example {
			margin-top: 100rpx;
			border: 1rpx solid #999;
			border-radius: 30rpx;
		}

		.example-text {
			margin-top: 10rpx;
			font-size: 28rpx;
			color: #999;
			text-align: center;
		}

		.btn {
			padding: 0 30rpx;
			width: 100%;
			position: fixed;
			bottom: 5%;
			left: 0;
		}
	}
</style>